<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>视觉小说Demo</title>
    <script src="assets/juzi.js"></script>
    <script src="token.js"></script>
    <script src="phaser.js"></script>
    <style>
        body
        {
            display: flex;
            justify-content: center;
        }
    </style>
    <link rel="shortcut icon" href="phaser-planet-web.png" type="image/x-icon">
</head>
<body>
    <!-- 参考代码写视觉小说 -->
    <!-- <h2>视觉小说Demo</h2> -->
    <script>
        var config = {
    type: Phaser.AUTO,
    width: 800,
    height: 600,
    backgroundColor: '#a0a0a0',
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};

var game = new Phaser.Game(config);
var eval_index = 0
var max_index = 0
var content = `@bgm cat
@bgm naycat
"您已创建一个新的phaser-novel游戏。"
"当您完善了故事、图片和音乐之后，您就可以向全世界发布了！"
`


function preload ()
{
    this.load.image('textbg',"gui/textbox.png")
    this.load.image('bg','bg/050.jpg')
    this.load.image('ch','assets/c01.png')
    this.load.image('ch2','assets/m01.png')
    this.load.audio('cat','assets/cat.mp3')
    this.load.audio('naycat','assets/naycat.mp3')
}

function create ()
{
    // console.log(this)
    // console.log(Phaser)
    const p = this
    const bg = this.add.image(0,0,'bg')
    bg.setOrigin(0)
    const ch = this.add.image(this.cameras.main.width / 2, this.cameras.main.height / 2, 'ch');
    ch.setOrigin(0.5);

    // ch.setTexture('ch2');

    const textbg = this.add.image(0,500,'textbg')
    textbg.setOrigin(0);

    // var text = new Phaser.GameObjects.Text(this, 100, 550, 'Hello, World!', { fontSize: '20px'});
    // this.add.existing(text);
    const dialog = this.add.text(100,500,"",{fontSize:'20px'})
    // const bgm = this.sound.add('bgm');
    

    var sp = split_script(content)
    max_index = sp.length

    
    // music.play();*/
    
    this.input.on('pointerdown', function (pointer)
    {
        // console.log('全局鼠标点击事件触发！');
        if(eval_index < max_index)
        {
            var each = sp[eval_index]
            if(each.startsWith("@"))
            {
                var ssp = each.split(" ")
                if(ssp[0] == "@bgm")
                {
                    // bgm = p.sound.add(ssp[1])
                    // bgm.play()
                }
            }
            else
            {
                dialog.text = each
            }
            eval_index+=1
        }
        
    });
    
    // 内置按钮 隐藏对话框
    function hide_dialog(p_object)
    { 
        p_object.visible = false
    }
    function show_dialog(p_object)
    {
        text.visible = true
    }
    function split_script(script)
    {
        return script.split("\n")
    }
}
function update ()
{
    // Phaser.
    //console.log("hello")
}
    </script>
</body>
</html>